package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.UserPhotoListAdapter;
import com.afd.ozblip.entity.BusinessPhotoEntity;
import com.afd.ozblip.entity.RecDishEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class UserPhotoActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private GridView photoGV;
	/****************** 数据变量 **************************/
	private List<RecDishEntity> photoList;
	private UserPhotoListAdapter photoAdapter;
	private String userId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_photo);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 获取数据
		requestPhotoListData();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.photoGV = (GridView) findViewById(R.id.gv_user_photo_list);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent settingIntent = new Intent(UserPhotoActivity.this,
							SettingActivity.class);
					startActivity(settingIntent);
					break;

				default:
					break;
				}
			}
		};
		this.photoGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Intent imageIntent = new Intent(
						UserPhotoActivity.this, ImageShowActivity.class);
				RecDishEntity rde = photoAdapter.dataList.get(position);
				imageIntent.putExtra("imageUrl", rde.getRecDishBigImageUrl());
				startActivity(imageIntent);
			}
			
		});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
		userId = intent.getStringExtra("userId");
	}

	/**
	 * 获取评论列表数据
	 */
	private void requestPhotoListData() {
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
						nearbyBusinessParamMap.put("userId", userId);
						nearbyBusinessParamMap.put("imgType", "0");
						nearbyBusinessParamMap.put("pageCnt", "1");
						nearbyBusinessParamMap.put("pageRow", "20");
						// 发送请求
						byte[] commentItemList = HttpUtil.requestByPost(
								UserPhotoActivity.this,
								Constants.USER_PHOTO_LIST_API,
								nearbyBusinessParamMap);
						if (commentItemList != null) {
							photoList = new ParseJson()
									.parseUserPhotoListJson(commentItemList);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setDishListAdapter();
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用菜单列表适配器
	 */
	public void setDishListAdapter() {
		if (CollectionUtil.UnNullAndZero(photoList)) {
			this.photoAdapter = new UserPhotoListAdapter(UserPhotoActivity.this);
			this.photoAdapter.addItems(photoList);
			this.photoGV.setAdapter(this.photoAdapter);
		}
	}
}